
using Xeralabs.GDESK.Events;
using Xeralabs.GDESK;
using Xeralabs.GDESK.Test.Entities;
using Xeralabs.GDESK.Utils.Internals;
using System.Threading;

namespace Xeralabs.GDESK.Test.GDESK
{
    class Test02TaskTime : iTask
    {
        private double _budget;

        public Test02TaskTime(double budget)
        {
            _budget = budget;
        }

        #region Miembros de iTask
        public double Budget { get { return _budget; } set { _budget = value; } }

        public void Execute(int id, double time, object arg)
        {
            System.Diagnostics.Debug.WriteLine(string.Format("TaskTime: {0}\t\tTime: {1}\t\tFrame:{2}", id, time, (ulong)arg));
        }
        #endregion
    }
    class Test02TaskFrame : iTask
    {
        private double _budget;

        public Test02TaskFrame(double budget)
        {
            _budget = budget;
        }

        #region Miembros de iTask
        public double Budget { get { return _budget; } set { _budget = value; } }

        public void Execute(int id, double time, object arg)
        {
            System.Diagnostics.Debug.WriteLine(string.Format("TaskFrame: {0}\t\tTime: {1}\t\tFrame: {2}", id, time, (ulong)arg));
        }
        #endregion
    }
    class Test02TaskIdle : iTask
    {
        private double _budget;

        public Test02TaskIdle(double budget)
        {
            _budget = budget;
        }

        #region Miembros de iTask
        public double Budget { get { return _budget; } set { _budget = value; } }

        public void Execute(int id, double time, object arg)
        {
            System.Diagnostics.Debug.WriteLine(string.Format("TaskIdle: {0}\t\tTime: {1}\t\tFrame:{2}", id, time, (ulong)arg));
        }
        #endregion
    }

    class Test02Kernel : XGDESK
    {
        public event Test02Delegate UpdateForm;
        private int task1, task2, task3, task4, task5, task6, task7, task8, task9, task0;

        public Test02Kernel()
            : base(1000000, 100000, 100000)
        {
        }

        protected override void WarmOperations()
        {
            task1 = this.Schedule(TaskType.TASK_TIME, 1000.0, 500.0, 0.0, new Test02TaskTime(30.0), null);

            task2 = this.Schedule(TaskType.TASK_TIME, 10000.0, 2000.0, 10000.0, new Test02TaskTime(10.0), null);

            task3 = this.Schedule(TaskType.TASK_TIME, 1000.0, 15000.0, 0.0, new Test02TaskTime(5.0), null);

            task4 = this.Schedule(TaskType.TASK_FRAME, 0.0, 10.0, 0.0, new Test02TaskFrame(10.0), null);

            task5 = this.Schedule(TaskType.TASK_FRAME, 10.0, 10.0, 1000.0, new Test02TaskFrame(15.0), null);

            task6 = this.Schedule(TaskType.TASK_FRAME, 10.0, 50.0, 1000.0, new Test02TaskFrame(20.0), null);

            task7 = this.Schedule(TaskType.TASK_TIME, 5000.0, 1000.0, 0.0, new Test02TaskTime(3.0), null);

            task8 = this.Schedule(TaskType.TASK_FRAME, 50.0, 25.0, 0.0, new Test02TaskFrame(15.0), null);

            //task9 = this.Schedule(TaskType.TASK_IDLE, 1000.0, 5000.0, 0.0, new Test02TaskTime(300.0), null);

            //task0 = this.Schedule(TaskType.TASK_IDLE, 1000.0, 5000.0, 0.0, new Test02TaskTime(300.0), null);
        }

        protected override void SaveState( )
        {
        }

        protected override void SaveStateAsync()
        {
        }

        protected override bool HandleWrongEvent(XGDESKEvent evt)
        {
            return false;
        }

        protected override void OnEntityRegistered(Xeralabs.GDESK.Entities.XGDESKEntity entity)
        {
            if (entity is Test02Entity && this.UpdateForm!=null)
            {
                ((Test02Entity)entity).UpdateForm += new Test02Delegate(UpdateForm);
            }
            if (this.IsRunning())
                entity.DoInitialEvents();
        }

        protected override void OnEntityRemoved(Xeralabs.GDESK.Entities.XGDESKEntity entity)
        {
            if (entity is Test02Entity && this.UpdateForm != null)
            {
                ((Test02Entity)entity).UpdateForm -= new Test02Delegate(UpdateForm);
            }
        }



    }
}
